package com.jx.controller;

import com.jx.entity.User;
import com.jx.service.UserService;
import com.jx.utils.StringUtil;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Date 2021/11/1 14:49
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController {

@Resource
    private UserService userService;


    /**
     * 跳转登录页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/loginForm")
    public String loginForm(Model model,HttpSession session){
        User user = (User)session.getAttribute("login_user");
        if (user != null){
            model.addAttribute("user",user);
            return "login/main";
        }
        return "login/loginForm";
    }

    /**
     * 登录
     * @param password
     * @param username
     * @return
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public Map<String,Object> login(HttpSession session,HttpServletRequest request,
                                    String code,String username, String password){
        Map<String,Object>map = new HashMap();
        if(StringUtil.isBlank(username) || StringUtil.isBlank(password)){
            System.out.println(username + ":" + password);
            map.put("blank","blank");
        } else {
            if (!CaptchaUtil.ver(code, request)) {
                CaptchaUtil.clear(request);
                map.put("yzm","yzm");
            } else{
                System.out.println(username + ":" + password);
//                User user = userService.getUserByName(username);
                User u = new User();
                u.setUsername(username);
                u.setPassword(password);
                List<User> userList = userService.getUserByUser(u);

                if(userList == null || userList.size() == 0){
                    map.put("error","error");
                } else{
                    session.setAttribute("login_user",userList.get(0));
                    map.put("success","success");
                }
            }

        }

        return map;
    }

    /**
     * 验证码
     * @param request
     * @param response
     */
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
        try {
            CaptchaUtil.out(request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 主页
     * @param model
     * @return
     */
    @RequestMapping(value = "/main")
    public String toUserListPage(Model model,HttpSession session){
        User user = (User)session.getAttribute("login_user");
        model.addAttribute("user",user);
        return "login/main";
    }


    /**
     * 跳转身份验证页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/checkForm")
    public String checkForm(Model model){
        return "login/checkForm";
    }


    /**
     * 身份验证
     * @return
     */
    @RequestMapping(value = "/checkUser")
    @ResponseBody
    public Map<String,Object> checkUser(User user){
        Map<String,Object>map = new HashMap<String,Object>();
        List<User> userList = userService.getUserByUser(user);
        if(userList != null && userList.size() > 0) {
            map.put("code", userList.size());
            map.put("id",userList.get(0).getId());
        }
        return map;
    }


    /**
     * 跳转重置密码页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/forgetPassword")
    public String forgetPassword(Model model,String id){
        model.addAttribute("id",id);
        return "login/forgetPassword";
    }

    /**
     * 登录
     * @return
     */
    @RequestMapping(value = "/resetPassword")
    @ResponseBody
    public Map<String,Object> resetPassword(User user){
        Map<String,Object>map = new HashMap<String,Object>();
        if(user == null || "".equals(user.getId())){
            map.put("code","blank");
            map.put("msg","信息有误，请联系管理员！！！");
            return map;
        }
        int i = userService.updateByPrimaryKeySelective(user);
        map.put("code",i);
        return map;
    }

    /**
     * 退出登入
     * @param model
     * @return
     */
    @RequestMapping(value = "/signOut")
    public String signOut(Model model,HttpSession session){
        session.removeAttribute("login_user");
//       session.invalidate();
        return "login/loginForm";
    }


}
